package com.app.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.app.biz.AppCategoryBiz;
import com.app.biz.AppInfoBiz;
import com.app.biz.AppVersionBiz;
import com.app.biz.DataDictionaryBiz;
import com.app.entity.AppCategory;
import com.app.entity.AppInfo;
import com.app.entity.DataDictionary;

@Controller
public class BackAppController {
	@Autowired
	private AppInfoBiz appInfobiz;

	@Autowired
	private AppCategoryBiz appCategoryBiz;

	@Autowired
	private DataDictionaryBiz dataDictionaryBiz;

	@Autowired
	private AppCategoryBiz appcategoryBiz;

	@Autowired
	private AppVersionBiz appVersionBiz;
	
	@RequestMapping("/backindex")
	public String backindex(Model model) {

		return "backindex";
	}
	
	@RequestMapping("/backinformation")
	public String information(Model model, Integer cid, Integer page,
			AppInfo appInfo) {
		cid = cid == null ? 0 : cid;
		page = page == null ? 1 : page;
		int pageSize = 5;
		int startIndex = (page - 1) * pageSize;

		List<AppInfo> list = appInfobiz.getAppInfoPageByStatus(0,
				appInfo.getCategoryLevel1(), appInfo.getCategoryLevel2(),
				appInfo.getCategoryLevel3(), appInfo.getSoftwareName(),
				appInfo.getStatus(), appInfo.getFlatformId(), startIndex,
				pageSize);

		List<DataDictionary> dataDictionaries = dataDictionaryBiz.getAll();
		model.addAttribute("dataDictionaries", dataDictionaries);

		int rows = appInfobiz.getRowsCount1(cid, appInfo.getCategoryLevel1(),
				appInfo.getCategoryLevel2(), appInfo.getCategoryLevel3(),
				appInfo.getSoftwareName(), appInfo.getStatus(),
				appInfo.getFlatformId());
		int pageCount = (int) Math.ceil((double) rows / pageSize);

		model.addAttribute("page", page);
		model.addAttribute("totalPages", pageCount);
		model.addAttribute("list", list);
		return "backinformation";
	}
	@RequestMapping(value = "/Level22", produces = { "application/json;charset=utf-8" })
	@ResponseBody
	public String getAllcategoryLevel2(Integer categoryLevel1) {
		System.out.println(categoryLevel1);
		List<AppCategory> list = appCategoryBiz
				.getAppCategoryByParendId(categoryLevel1);
		String result = JSON.toJSONString(list, true);
		return result;
	}

	@RequestMapping(value = "/Level33", produces = { "application/json;charset=utf-8" })
	@ResponseBody
	public String getAllcategoryLevel3(Integer categoryLevel2) {
		System.out.println(categoryLevel2);
		List<AppCategory> list = appCategoryBiz
				.getAppCategoryByParendId(categoryLevel2);
		String result = JSON.toJSONString(list, true);
		return result;
	}
	@RequestMapping("/updateInfoByStatus")
	public String updateInfoByStatus(Integer id,Model model, Integer cid, Integer page,
			AppInfo appInfo){
		
		appInfobiz.updateInfoByStatus(id);
		
		cid = cid == null ? 0 : cid;
		page = page == null ? 1 : page;
		int pageSize = 5;
		int startIndex = (page - 1) * pageSize;

		List<AppInfo> list = appInfobiz.getAppInfoPageByStatus(0,
				appInfo.getCategoryLevel1(), appInfo.getCategoryLevel2(),
				appInfo.getCategoryLevel3(), appInfo.getSoftwareName(),
				appInfo.getStatus(), appInfo.getFlatformId(), startIndex,
				pageSize);

		List<DataDictionary> dataDictionaries = dataDictionaryBiz.getAll();
		model.addAttribute("dataDictionaries", dataDictionaries);

		int rows = appInfobiz.getRowsCount1(cid, appInfo.getCategoryLevel1(),
				appInfo.getCategoryLevel2(), appInfo.getCategoryLevel3(),
				appInfo.getSoftwareName(), appInfo.getStatus(),
				appInfo.getFlatformId());
		int pageCount = (int) Math.ceil((double) rows / pageSize);

		model.addAttribute("page", page);
		model.addAttribute("totalPages", pageCount);
		model.addAttribute("list", list);
		return "redirect:backinformation";
	}
}
